/*
 * Copyright (c) 2011, Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.util;

import java.util.HashMap;
import java.util.Map;

import org.jowidgets.common.types.VirtualKey;

import com.trolltech.qt.core.Qt;

public final class VirtualKeyConvert {

	private static Map<VirtualKey, Qt.Key> virtualKeyToKeyCode;
	private static Map<Qt.Key, VirtualKey> keyCodeToVirtualKey;

	private VirtualKeyConvert() {};

	public static int convert(final VirtualKey virtualKey) {
		int result;

		if (virtualKey.isCharacter()) {
			result = virtualKey.getCharacter().charValue();
		} else {
			result = convertVirtualKeyToKeyCode(virtualKey);
		}

		return result;
	}

	public static VirtualKey convert(final int keyCode) {
		VirtualKey result;
		if (keyCode == 0) {
			result = VirtualKey.UNDEFINED;
		} else {
			final Qt.Key key = Qt.Key.resolve(keyCode);
			result = getKeyCodeToVirtualKey().get(key);
			if (result == null) {
				result = VirtualKey.toVirtualKey((char) keyCode);
			}
		}
		if (result == null) {
			result = VirtualKey.UNDEFINED;
		}
		return result;
	}

	private static int convertVirtualKeyToKeyCode(final VirtualKey virtualKey) {
		final Qt.Key result = getVirtualKeyToKeyCode().get(virtualKey);
		if (result == null) {
			throw new IllegalArgumentException("The virtual key '" + virtualKey + "' is unkown.");
		} else {
			return result.value();
		}
	}

	private static Map<VirtualKey, Qt.Key> getVirtualKeyToKeyCode() {
		if (virtualKeyToKeyCode == null) {
			virtualKeyToKeyCode = new HashMap<VirtualKey, Qt.Key>();
			virtualKeyToKeyCode.put(VirtualKey.F1, Qt.Key.Key_F1);
			virtualKeyToKeyCode.put(VirtualKey.F2, Qt.Key.Key_F2);
			virtualKeyToKeyCode.put(VirtualKey.F3, Qt.Key.Key_F3);
			virtualKeyToKeyCode.put(VirtualKey.F4, Qt.Key.Key_F4);
			virtualKeyToKeyCode.put(VirtualKey.F5, Qt.Key.Key_F5);
			virtualKeyToKeyCode.put(VirtualKey.F6, Qt.Key.Key_F6);
			virtualKeyToKeyCode.put(VirtualKey.F7, Qt.Key.Key_F7);
			virtualKeyToKeyCode.put(VirtualKey.F8, Qt.Key.Key_F8);
			virtualKeyToKeyCode.put(VirtualKey.F9, Qt.Key.Key_F9);
			virtualKeyToKeyCode.put(VirtualKey.F10, Qt.Key.Key_F10);
			virtualKeyToKeyCode.put(VirtualKey.F11, Qt.Key.Key_F11);
			virtualKeyToKeyCode.put(VirtualKey.F12, Qt.Key.Key_F12);

			virtualKeyToKeyCode.put(VirtualKey.ENTER, Qt.Key.Key_Enter);
			virtualKeyToKeyCode.put(VirtualKey.BACK_SPACE, Qt.Key.Key_Backspace);
			virtualKeyToKeyCode.put(VirtualKey.TAB, Qt.Key.Key_Tab);
			virtualKeyToKeyCode.put(VirtualKey.SHIFT, Qt.Key.Key_Shift);
			virtualKeyToKeyCode.put(VirtualKey.CONTROL, Qt.Key.Key_Control);
			virtualKeyToKeyCode.put(VirtualKey.ALT, Qt.Key.Key_Alt);
			virtualKeyToKeyCode.put(VirtualKey.PAUSE, Qt.Key.Key_Pause);
			virtualKeyToKeyCode.put(VirtualKey.CAPS_LOCK, Qt.Key.Key_CapsLock);
			virtualKeyToKeyCode.put(VirtualKey.ESC, Qt.Key.Key_Escape);
			virtualKeyToKeyCode.put(VirtualKey.SPACE, Qt.Key.Key_Space);
			virtualKeyToKeyCode.put(VirtualKey.PAGE_UP, Qt.Key.Key_PageUp);
			virtualKeyToKeyCode.put(VirtualKey.PAGE_DOWN, Qt.Key.Key_PageDown);
			virtualKeyToKeyCode.put(VirtualKey.END, Qt.Key.Key_End);
			virtualKeyToKeyCode.put(VirtualKey.HOME, Qt.Key.Key_Home);

			virtualKeyToKeyCode.put(VirtualKey.ARROW_LEFT, Qt.Key.Key_Left);
			virtualKeyToKeyCode.put(VirtualKey.ARROW_UP, Qt.Key.Key_Up);
			virtualKeyToKeyCode.put(VirtualKey.ARROW_RIGHT, Qt.Key.Key_Right);
			virtualKeyToKeyCode.put(VirtualKey.ARROW_DOWN, Qt.Key.Key_Down);
		}
		return virtualKeyToKeyCode;
	}

	private static Map<Qt.Key, VirtualKey> getKeyCodeToVirtualKey() {
		if (keyCodeToVirtualKey == null) {
			keyCodeToVirtualKey = new HashMap<Qt.Key, VirtualKey>();
			keyCodeToVirtualKey.put(Qt.Key.Key_F1, VirtualKey.F1);
			keyCodeToVirtualKey.put(Qt.Key.Key_F2, VirtualKey.F2);
			keyCodeToVirtualKey.put(Qt.Key.Key_F3, VirtualKey.F3);
			keyCodeToVirtualKey.put(Qt.Key.Key_F4, VirtualKey.F4);
			keyCodeToVirtualKey.put(Qt.Key.Key_F5, VirtualKey.F5);
			keyCodeToVirtualKey.put(Qt.Key.Key_F6, VirtualKey.F6);
			keyCodeToVirtualKey.put(Qt.Key.Key_F7, VirtualKey.F7);
			keyCodeToVirtualKey.put(Qt.Key.Key_F8, VirtualKey.F8);
			keyCodeToVirtualKey.put(Qt.Key.Key_F9, VirtualKey.F9);
			keyCodeToVirtualKey.put(Qt.Key.Key_F10, VirtualKey.F10);
			keyCodeToVirtualKey.put(Qt.Key.Key_F11, VirtualKey.F11);
			keyCodeToVirtualKey.put(Qt.Key.Key_F12, VirtualKey.F12);

			keyCodeToVirtualKey.put(Qt.Key.Key_Enter, VirtualKey.ENTER);
			keyCodeToVirtualKey.put(Qt.Key.Key_Backspace, VirtualKey.BACK_SPACE);
			keyCodeToVirtualKey.put(Qt.Key.Key_Tab, VirtualKey.TAB);
			keyCodeToVirtualKey.put(Qt.Key.Key_Shift, VirtualKey.SHIFT);
			keyCodeToVirtualKey.put(Qt.Key.Key_Control, VirtualKey.CONTROL);
			keyCodeToVirtualKey.put(Qt.Key.Key_Alt, VirtualKey.ALT);
			keyCodeToVirtualKey.put(Qt.Key.Key_Pause, VirtualKey.PAUSE);
			keyCodeToVirtualKey.put(Qt.Key.Key_CapsLock, VirtualKey.CAPS_LOCK);
			keyCodeToVirtualKey.put(Qt.Key.Key_Escape, VirtualKey.ESC);
			keyCodeToVirtualKey.put(Qt.Key.Key_Space, VirtualKey.SPACE);
			keyCodeToVirtualKey.put(Qt.Key.Key_PageUp, VirtualKey.PAGE_UP);
			keyCodeToVirtualKey.put(Qt.Key.Key_PageDown, VirtualKey.PAGE_DOWN);
			keyCodeToVirtualKey.put(Qt.Key.Key_End, VirtualKey.END);
			keyCodeToVirtualKey.put(Qt.Key.Key_Home, VirtualKey.HOME);

			keyCodeToVirtualKey.put(Qt.Key.Key_Left, VirtualKey.ARROW_LEFT);
			keyCodeToVirtualKey.put(Qt.Key.Key_Up, VirtualKey.ARROW_UP);
			keyCodeToVirtualKey.put(Qt.Key.Key_Right, VirtualKey.ARROW_RIGHT);
			keyCodeToVirtualKey.put(Qt.Key.Key_Down, VirtualKey.ARROW_DOWN);
		}
		return keyCodeToVirtualKey;
	}
}
